package wangjg.android.service;

import wangjg.android.R;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.os.Binder;
import android.os.IBinder;
import android.os.Parcel;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.WindowManager.LayoutParams;

/**
 * 悬浮窗Service 该服务会在后台一直运行一个悬浮的透明的窗体。
 * 
 * @author
 * 
 */
public class FloatingService extends Service {

	private int statusBarHeight;// 状态栏高度
	private View view;// 透明窗体
	private WindowManager windowManager;
	private WindowManager.LayoutParams layoutParams;

	@Override
	public void onCreate() {
		super.onCreate();
		view = LayoutInflater.from(this).inflate(R.layout.chat_item, null);
		layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT, LayoutParams.TYPE_SYSTEM_ALERT,
				LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSPARENT);
		layoutParams.gravity = Gravity.LEFT | Gravity.TOP;

		this.view.setOnTouchListener(viewTouchListener);

	}
	
	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		this.addView();
	}
	
	private WindowManager getWindowManager(){
		if(windowManager==null){
			windowManager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
		}
		return windowManager;
	}

	public void setView(View view){
		if(this.view!=null){
			this.removeView();
		}
		this.view = view;
		this.view.setOnTouchListener(viewTouchListener);
		this.addView();
	}
	
	private void addView(){
		getWindowManager().addView(view, layoutParams);
	}
	
	private void removeView() {
		if (view!=null) {
			getWindowManager().removeView(view);
		}
	}

	/*
	 * 刷新悬浮窗
	 * 
	 * @param x 拖动后的X轴坐标
	 * @param y 拖动后的Y轴坐标
	 */
	private void refreshView(int x, int y) {
		if(view==null){return;}
		// 状态栏高度不能立即取，不然得到的值是0
		if (statusBarHeight == 0) {
			View rootView = view.getRootView();
			Rect r = new Rect();
			rootView.getWindowVisibleDisplayFrame(r);
			statusBarHeight = r.top;
		}
	
		layoutParams.x = x;
		// y轴减去状态栏的高度，因为状态栏不是用户可以绘制的区域，不然拖动的时候会有跳动
		layoutParams.y = y - statusBarHeight;// STATUS_HEIGHT;
		
		windowManager.updateViewLayout(view, layoutParams);
	}

	private OnTouchListener viewTouchListener = new OnTouchListener() {
		float[] temp = new float[] { 0f, 0f };
	
		public boolean onTouch(View v, MotionEvent event) {
			layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
			int eventaction = event.getAction();
			switch (eventaction) {
			case MotionEvent.ACTION_DOWN: // 按下事件，记录按下时手指在悬浮窗的XY坐标值
				temp[0] = event.getX();
				temp[1] = event.getY();
				break;
	
			case MotionEvent.ACTION_MOVE:
				refreshView((int) (event.getRawX() - temp[0]),(int) (event.getRawY() - temp[1]));
				break;
	
			}
			return true;
		}
	};

	@Override
	public void onDestroy() {
		this.removeView();
		super.onDestroy();
	}

	@Override
	public IBinder onBind(Intent intent) {
		return new MyBinder();
	}

	public boolean onUnbind(Intent intent){
		return super.onUnbind(intent);
	}

	class StatusBarReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// intent.get
		}

	}
	
    public class MyBinder extends Binder {
    	
 	    public FloatingService getService() {  
	        return FloatingService.this;  
	    }
 	    
    	//这个方法主要是接收Activity发向服务的消息，data为发送消息时向服务传入的对象，replay是由服务返回的对象  
    	 public boolean onTransact( int code , Parcel data , Parcel reply , int flags )throws android.os.RemoteException{  
    	        //called when client calls transact on returned Binder
    	        return transact( code , data , reply , flags );  
    	    }  
    	} 
}